package com.shtel.paas.monitorcenter.view.dispaly.host.monitor.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shtel.paas.monitorcenter.view.dispaly.host.monitor.entity.SearchServerInfoEntity;
import com.shtel.paas.monitorcenter.view.dispaly.host.monitor.mapper.HostMonitorMapper;
import com.shtel.paas.monitorcenter.view.dispaly.host.monitor.service.HostMonitorService;
import com.shtel.paas.monitorcenter.view.dispaly.host.monitor.vo.SearchServerInfoVo;
import com.shtel.paas.monitorcenter.view.dispaly.host.monitor.vo.selecthostMonitorTreeDataVo;

/**
 * 主机监控
 * 
 * @author zhaochuan.zhong
 *
 */

@Service
public class HostMonitorServiceImpl implements HostMonitorService {

	@Autowired
	HostMonitorMapper hostMonitorMapper;

	/**
	 * 获取每台服务器的基本信息
	 */
	@Override
	public SearchServerInfoVo selectServiceInfo(SearchServerInfoEntity rmpEnt) {
		SearchServerInfoVo resultArray = new SearchServerInfoVo();
		resultArray = this.hostMonitorMapper.selectServiceInfo(rmpEnt);
		return resultArray;
	}

	/**
	 * 获取Grafana信息
	 */
	@Override
	public String getGrafanaUrl(Integer id) {
		String url = this.hostMonitorMapper.getGrafanaUrl(id);
		return url;
	}

	/**
	 * 动态获取动态树的第一到第二层节点数据
	 */
	@Override
	public List<Object> selecthostMonitorTreeData() {
		// 从数据库取出的原始数据
		List<selecthostMonitorTreeDataVo> resultArray = new ArrayList<>();
		// 第一层数据
		List<selecthostMonitorTreeDataVo> parentArrayList = new ArrayList<>();
		// 第二层数据
		List<selecthostMonitorTreeDataVo> childArray = new ArrayList<>();
		// 返回结果
		List<Object> result = new ArrayList<>();
		resultArray = hostMonitorMapper.selecthostMonitorTreeData();

		// 第一层节点数据和第二层节点数据分类
		for (selecthostMonitorTreeDataVo parent : resultArray) {
			if (parent.getCommentId() != null) {
				parent.setIsParent("true");
				if (parent.getCommentId().equals(parent.getParentId())) {// 第一层数据
					parent.setIsParent("true");
					parentArrayList.add(parent);
				} else {// 第二层数据
					childArray.add(parent);
				}
			}
		}

		// tree数据改造
		for (selecthostMonitorTreeDataVo parent : parentArrayList) {
			List<selecthostMonitorTreeDataVo> childList = new ArrayList<>();
			Map<String, Object> results = new HashMap<String, Object>();
			for (selecthostMonitorTreeDataVo child : childArray) {
				if (parent.getParentId().equals(child.getParentId())) {
					childList.add(child);
				}
			}
			results.put("name", parent.getName());
			results.put("children", childList);
			results.put("isParent", true);
			result.add(results);
		}
		return result;
	}

	/**
	 * 异步获取动态树的第三层节点数据
	 */
	@Override
	public List<selecthostMonitorTreeDataVo> asyncSelectHostMonitorTreeData(
			String commentId) {

		List<selecthostMonitorTreeDataVo> resultList = hostMonitorMapper
				.asyncSelectHostMonitorTreeData(commentId);

		return resultList;
	}

}
